﻿// Apache-2.0 License
// Copyright (c) 2021-2022 SuperJeff
// 微信:15906588664  QQ:1024489


using iTextSharp.text.pdf;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.Versioning;

namespace Admin.Net.InformationPlatform.Util;
/// <summary>
/// 添加水印
/// </summary>
public class Watermark
{
    /// <summary>
    /// 添加文本水印
    /// </summary>
    /// <param name="pdfStream">pdf文件</param>
    /// <param name="addText">水印文字</param>
    public static MemoryStream PdfAddWatermark(MemoryStream pdfStream, string addText)
    {
        //读取pdf
        MemoryStream outStream = new MemoryStream();
        PdfReader reader = new PdfReader(pdfStream);
        //创建新pdf
        PdfStamper stamper = new PdfStamper(reader, outStream);
        int pdfTotalPage = reader.NumberOfPages;//总页数
                                                //第一页pdf尺寸
        iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
        float width = psize.Width;
        float height = psize.Height;

        iTextSharp.text.pdf.PdfContentByte content;
        //【C:\WINDOWS\Fonts】系统字体存放位置
        iTextSharp.text.pdf.BaseFont font = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED);


        iTextSharp.text.pdf.PdfGState gs = new iTextSharp.text.pdf.PdfGState();

        var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        for (int i = 1; i <= pdfTotalPage; i++)
        {
            //GetUnderContent内容下层
            //GetOverContent内容上层
            content = stamper.GetOverContent(i);

            //content = pdfStamper.GetOverContent(i);//在内容上方加水印
            //content = pdfStamper.GetUnderContent(i);//在内容下方加水印
            //透明度
            gs.FillOpacity = 0.3f;
            content.SetGState(gs);
            //content.SetGrayFill(0.3f);
            //开始写入文本
            content.BeginText();
            //设置颜色
            content.SetColorFill(iTextSharp.text.BaseColor.RED);
            //字体大小
            content.SetFontAndSize(font, 30);
            //设置文本矩阵
            content.SetTextMatrix(0, 0);
            //水印文本位置
            //中间
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 2, height / 2, 45);
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 2, height / 10, 45);
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 2, height - height / 10, 45);
            //右侧
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 10, height / 2, 45);
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 10, height / 10, 45);
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 10, height - 360, 45);
            //左侧
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - width / 10, height / 2, 45);
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - width / 10, height / 10, 45);
            content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - width / 10, height - height / 10, 45);
            content.EndText();
        }
        stamper.Close();
        reader.Close();

        return outStream;
    }


    /// <summary>
    /// 给图片加水印
    /// </summary>
    /// <param name="imageStream">图片文件流</param>
    /// <param name="addText">水印文字</param>
    /// <returns></returns>
    [SupportedOSPlatform("windows")]
    public static MemoryStream ImageAddWatermark(MemoryStream imageStream, string addText)
    {
        //创建一个image对象，用来装载要被添加水印的图片
        System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
        //获取图片的宽和高
        int width = image.Width;
        int height = image.Height;
        //创建一个bitmap对象
        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        //创建一个绘图板，从bitmap对象生成
        Graphics graphics = Graphics.FromImage(bitmap);
        //设置绘图板的描绘质量
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        //将原图拷贝到绘图板中
        graphics.DrawImage(image, 0, 0, width, height);
        //创建Font对象，参数分别是字体名称，字体大小，字体样式
        System.Drawing.Font font = new System.Drawing.Font("宋体", 30, FontStyle.Bold);
        var color = System.Drawing.Color.FromArgb(50, System.Drawing.Color.Red); // 50% 透明度的红色
        //创建一个Brush对象，参数为字体颜色
        Brush brush = new SolidBrush(color);
        //设置字体在图片中的位置（水印位置）
        graphics.RotateTransform(10);
        //获取水印长宽
        var watermarkSize = graphics.MeasureString(addText, font);
        var watermarkWidth = (int)watermarkSize.Width;
        var watermarkHeight = (int)watermarkSize.Height * 2;

        // 循环绘制水印文本
        for (int y = 0; y < image.Height; y += watermarkHeight)
        {
            // 循环绘制水印文本
            for (int x = 0; x < image.Width; x += watermarkWidth)
            {
                // 绘制水印文本
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                // 添加水印文本到图片
                graphics.DrawString(addText, font, brush, x, y);

            }
        }

        graphics.ResetTransform();
        //保存操作
        MemoryStream newImageStream = new MemoryStream();
        bitmap.Save(newImageStream, ImageFormat.Jpeg);
        //释放资源
        bitmap.Dispose();
        image.Dispose();
        graphics.Dispose();
        return newImageStream;
    }
}
